package com.michael.study.customview.animation

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import com.michael.study.customview.R
import com.michael.study.customview.databinding.ActivityInterpolatorBinding

/**
 *
AccelerateDecelerateInterpolator   在动画开始与結束的地方速率改变比较慢，在中间的时候加速
AccelerateInterpolator                     在动画开始的地方速率改变比较慢，然后开始加速
AnticipateInterpolator                      开始的时候向后然后向前甩
AnticipateOvershootInterpolator     开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator                          动画结束的时候弹起
CycleInterpolator                             动画循环播放特定的次数，速率改变沿着正弦曲线
DecelerateInterpolator                    在动画开始的地方快然后慢
LinearInterpolator                            以常量速率改变
OvershootInterpolator                      向前甩一定值后再回到原来位置
 */
class InterpolatorActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var mBinding:ActivityInterpolatorBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityInterpolatorBinding.inflate(layoutInflater)
        setContentView(mBinding.root)

        mBinding.btnAccelerateDecelerate.setOnClickListener(this)
        mBinding.btnAccelerate.setOnClickListener(this)
        mBinding.btnAnticipate.setOnClickListener(this)
        mBinding.btnAnticipateOvershoot.setOnClickListener(this)
        mBinding.btnBounce.setOnClickListener(this)
        mBinding.btnCycle.setOnClickListener(this)
        mBinding.btnDecelerate.setOnClickListener(this)
        mBinding.btnLinear.setOnClickListener(this)
        mBinding.btnOvershoot.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v?.id) {
            R.id.btn_AccelerateDecelerate -> {
                doAccelerateDecelerate()
            }
            R.id.btn_Accelerate -> {
                doAccelerate()
            }
            R.id.btn_Anticipate -> {
                doAnticipate()
            }
            R.id.btn_AnticipateOvershoot -> {
                doAnticipateOverShot()
            }
            R.id.btn_Bounce -> {
                doBounce()
            }
            R.id.btn_Cycle -> {
                doCycle()
            }
            R.id.btn_Decelerate -> {
                doDecelerate()
            }
            R.id.btn_Linear -> {
                doLinear()
            }
            R.id.btn_Overshoot -> {
                doOverShot()
            }
        }
    }

    private fun doAccelerateDecelerate() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.scale_test_accel_deccel)
        mBinding.view.startAnimation(animation)
    }

    private fun doAccelerate() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.alpha_test_acce)
        mBinding.view.startAnimation(animation)
    }

    private fun doAnticipate() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.translate_test_anti)
        mBinding.view.startAnimation(animation)
    }

    private fun doAnticipateOverShot() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.translate_test_anti_overshot)
        mBinding.view.startAnimation(animation)
    }

    private fun doBounce() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.translate_test_bounce)
        mBinding.view.startAnimation(animation)
    }

    private fun doCycle() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.translate_test_cycle)
        mBinding.view.startAnimation(animation)
    }

    private fun doDecelerate() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.translate_test_decelerate)
        mBinding.view.startAnimation(animation)
    }

    private fun doLinear() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.translate_test_linear)
        mBinding.view.startAnimation(animation)
    }

    private fun doOverShot() {
        var animation = AnimationUtils.loadAnimation(this,R.anim.translate_test_overshot)
        mBinding.view.startAnimation(animation)
    }
}